package com.qf.czh.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.czh.mapper.UserMapper;
import com.qf.czh.model.AnnualLeave;
import com.qf.czh.model.User;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public PageInfo getUserByPage(int page, int rows, String sort , String order,String realname) {
        Map<String , Object> params = new HashMap<String, Object>();
        params.put("sort", sort);
        params.put("order", order);
        params.put("realname", realname);

        PageHelper.startPage(page, rows);
        List<User> allUser = userMapper.getAllUser(params);
        PageInfo<User> pageInfo = new PageInfo<User>(allUser);
//        System.out.println(pageInfo);
        return pageInfo;
    }


    public Integer addUser(String username, String realname, String gender, String birthday, String department, String rank) {
        Md5Hash pwd = new Md5Hash("123");//密码加密
        String password = pwd.toString();

        Map<String , Object> params = new HashMap<String, Object>();
        params.put("username", username);
        params.put("realname", realname);
        params.put("gender", gender);
        params.put("birthday", birthday);
        params.put("deptId", department);
        params.put("rank", rank);
        params.put("password", password);


        try {
            Integer r = userMapper.addUser(params);
            return r;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Integer updateUser(Integer id, String username,String realname, String gender, String birthday, String department, String rank) {
        Map<String , Object> params = new HashMap<String, Object>();
        params.put("username", username);
        params.put("realname", realname);
        params.put("gender", gender);
        params.put("birthday", birthday);
        params.put("deptId", department);
        params.put("rank", rank);
        params.put("id", id);

        try {
            Integer r = userMapper.updateUser(params);
            return r;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Integer deleteUser(String[] ids) {
        List<String> list = Arrays.asList(ids);
        try {
            userMapper.deleteUser(list);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }
}
